﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TracNghiemTinHocDaiCuong.BIZ;
using TracNghiemTinHocDaiCuong.Common;
using TracNghiemTinHocDaiCuong.ThucThe;

namespace TracNghiemTinHocDaiCuong
{
    public partial class QuanLyDeThi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LayDanhSach();
        }

        protected void btnThemDeThi_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThemDeThi.aspx");
        }

        protected void btnSuaDeThi_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("SuaDeThi.aspx?ID={0}", ViewState["idDeThi"]));
        }

        protected void btnXoaDeThi_Click(object sender, EventArgs e)
        {
            DeThiBIZ.Xoa(Methods.ConvertNumber(ViewState["idDeThi"].ToString()));
            LayDanhSach();
        }

        protected void lkbtnDeThi_Click(object sender, CommandEventArgs e)
        {
            ViewState["idDeThi"] = Methods.ConvertNumber(e.CommandName);
            int i = Methods.ConvertNumber(e.CommandArgument.ToString());
            gvDanhSachDeThi.Rows[i].Cells[0].CssClass = "row";
            gvDanhSachDeThi.Rows[i].Cells[1].CssClass = "row";
            gvDanhSachDeThi.Rows[i].Cells[2].CssClass = "row";
            gvDanhSachDeThi.Rows[i].Cells[3].CssClass = "row";
            gvDanhSachDeThi.Rows[i].Cells[4].CssClass = "row";
            gvDanhSachDeThi.Rows[i].Cells[5].CssClass = "row";
            gvDanhSachDeThi.Rows[i].Cells[6].CssClass = "row";
            gvDanhSachDeThi.Rows[i].Cells[7].CssClass = "row";
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            //IList<DeThi> danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text, hdfFilter.Value);
            //BindDuLieu_HienThiThongBao(danhSachDeThi);
        }

        protected void btnXoaTuKhoa_Click(object sender, EventArgs e)
        {
            txtNoiDung.Text = "";
        }

        protected void ClickFilter(object sender, CommandEventArgs e)
        {
            hdfFilter.Value = e.CommandName;
            LocDanhSachDeThi(e.CommandName);
        }

        private void LocDanhSachDeThi(string phamVi)
        {
            DataTable danhSachDeThi = null;
            switch (phamVi)
            {
                case "All":
                    {
                        danhSachDeThi = DeThiBIZ.LayDanhSach();
                        break;
                    }
                case "09":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text, "[0-9]%");
                        break;
                    }
                case "AE":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text, "[A-E]%");
                        break;
                    }
                case "FJ":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text, "[F-J]%");
                        break;
                    }
                case "KN":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text, "[K-N]%");
                        break;
                    }
                case "OR":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text, "[O-R]%");
                        break;
                    }
                case "SV":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text, "[S-V]%");
                        break;
                    }
                case "WZ":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text, "[W-Z]%");
                        break;
                    }
            }
            BindDuLieu_HienThiThongBao(danhSachDeThi);
        }

        protected void gvDanhSachDeThi_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDanhSachDeThi.PageIndex = e.NewPageIndex;
            gvDanhSachDeThi.DataBind();
        }

        private void BindDuLieu_HienThiThongBao(DataTable danhSachDeThi)
        {
            if (danhSachDeThi == null || danhSachDeThi.Rows.Count == 0)
                lblThongBao.Text = "Chưa có bản ghi để hiển thị";
            else
                lblThongBao.Text = "";
            gvDanhSachDeThi.DataSource = danhSachDeThi;
            gvDanhSachDeThi.DataBind();
        }

        private void LayDanhSach()  
        {
            DataTable deThi = DeThiBIZ.LayDanhSachTheoChuDe();
            if (deThi.Rows.Count == 0)
                lblThongBao.Text = "Chưa có bản ghi để hiển thị"; 
            else
                lblThongBao.Text = "";
            gvDanhSachDeThi.DataSource = deThi;
            gvDanhSachDeThi.DataBind();
            //for (int i = 0; i < gvDanhSachDeThi.Rows.Count; i++)
            //{
            //    TextBox txt = (TextBox)gvDanhSachDeThi.Rows[i].FindControl("DoKho");
            //    txt.Text = "Dễ";
            //}
        }
    }
}